﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class user_profile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        ctp_lib.check_login(this.Page, "user/profile.aspx",false);

        if (!this.IsPostBack)
        {
            ctpDataContext db = new ctpDataContext();
            user myuser = db.users.Single(p => p.no == (int)Session["userno"]);

            if (myuser.nickname != null)
                this.Label1.Text = myuser.nickname;

            if (myuser.intro != null)
            {
                this.TextBox5.Text = myuser.intro.Trim();
            }

            if (myuser.profile_picture == null)
                this.Image1.Visible = false;
            else
            {
                if (myuser.profile_picture.Trim() == null)
                    this.Image1.Visible = false;
                else
                {
                    this.Image1.Visible = true;
                    this.Image1.ImageUrl = "../user_imgs/1/" + myuser.profile_picture.Trim();
                }
            }

        }


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.TextBox1.Text.Trim() == "") return;

        ctpDataContext db = new ctpDataContext();
        user myuser=db.users.Single(p=>p.no==(int)Session["userno"]);

        myuser.nickname = this.TextBox1.Text;

        db.SubmitChanges();

        Response.Redirect("profile.aspx");

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (!this.FileUpload1.HasFile) return;

        string path = Page.MapPath("../user_imgs/");

        System.IO.FileInfo fi = new System.IO.FileInfo(this.FileUpload1.FileName);

        string ext = fi.Extension.ToLower();
        if (ext == ".jpeg") ext = ".jpg";
        if (ext != ".jpg" && ext != ".gif") return;

        string fn = Session["userno"].ToString() + ext;
        ctp_lib.savepic(this.Page, fn, this.FileUpload1.PostedFile, path);

        ctpDataContext db = new ctpDataContext();
        user myuser = db.users.Single(p => p.no == (int)Session["userno"]);
        myuser.profile_picture = fn;

        db.SubmitChanges();

        Response.Redirect("profile.aspx");

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        ctpDataContext db = new ctpDataContext();
        user myuser = db.users.Single(p => p.no == (int)Session["userno"]);

        if (myuser.pwd.Trim() != this.TextBox2.Text)
        {
            this.Label2.Text = "Your old password doesn't match, please try again.";
            this.Label2.Visible = true;
            return;
        }

        myuser.pwd = this.TextBox3.Text;
        db.SubmitChanges();

        this.Label2.Text = "Successfully changed.";

        //Response.Redirect("profile.aspx");

    }
    protected void Button4_Click(object sender, EventArgs e)
    {

        if (this.TextBox5.Text.Trim() == "") return;

        ctpDataContext db = new ctpDataContext();
        user myuser = db.users.Single(p => p.no == (int)Session["userno"]);

        myuser.intro = this.TextBox5.Text;
       // myuser.nickname = this.TextBox1.Text;

        db.SubmitChanges();

        Response.Redirect("profile.aspx");


    }
}
